/**
 * MIT License
 * 
 * Copyright (c) 2017 CaiDongyu
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.tunnel.common.http;

public class HttpPack {

	private byte[] header;//有可能会是整个数据，但是至少是包含header部分的
	private String headerUpperStr;
	private int headerLength = 0;
	private int contentLenType = 0;//0.说明没有类型，就是body是空的 另外两种类型， 1.Content-Length 和 2.Transfer-Encoding 3.connection:close
	private Integer contentLength = null;
	private byte[] body;
	
	public byte[] getHeader() {
		return header;
	}
	
	public void setHeader(byte[] header) {
		this.header = header;
		this.headerUpperStr = new String(header).toUpperCase();
		int cttIndex = getFieldIndexInHeader("CONTENT-LENGTH:");
		if(cttIndex > 0){
			int cttIndexEnd = headerUpperStr.indexOf("\r\n", cttIndex);
			String cl = headerUpperStr.substring(cttIndex + "CONTENT-LENGTH:".length(), cttIndexEnd);
			contentLenType = 1;
			contentLength = Integer.parseInt(cl.trim());
		}else if(getFieldIndexInHeader("TRANSFER-ENCODING:") > 0){
			contentLenType = 2;
		}else if(headerUpperStr.indexOf("CONNECTION:CLOSE") > 0 || headerUpperStr.indexOf("CONNECTION: CLOSE") > 0){
			contentLenType = 3;
		}
	}
	
	public void setBody(byte[] body) {
		this.body = body;
	}
	
	public byte[] getBody() {
		return body;
	}

	public int getContentLenType() {
		return contentLenType;
	}

	public Integer getContentLength() {
		return contentLength;
	}

	public int getHeaderLength() {
		return headerLength;
	}

	public void setHeaderLength(int headerLength) {
		this.headerLength = headerLength;
	}

	public int getFieldIndexInHeader(String field) {
		return headerUpperStr.indexOf(field.toUpperCase());
	}
	
}
